package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

//   保存菜品和口味信息
    @Transactional
    @Override
    public void saveWithFlaVor(DishDto dishDto) {
//        先保存菜品基本信息到菜品表
        this.save(dishDto);
//        再保存口味信息到口味表，注意添加外键dish_id
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            dishFlavorService.save(flavor);
        }

//        老师代码
        /*List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map(new Function<DishFlavor, DishFlavor>() {
            @Override
            public DishFlavor apply(DishFlavor dishFlavor) {
                dishFlavor.setDishId(dishDto.getId());
                return dishFlavor;
            }
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(collect);

        可以简写成
                List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map((flavor) -> {
            flavor.setDishId(dishDto.getId());
            return flavor;
        }).collect(Collectors.toList());

        */
    }
//   根据菜品id查询菜品信息和对应的口味信息
    @Override
    public DishDto getByIdWithFlavor(Long id) {
//        获取菜品信息
        Dish dish = this.getById(id);
//        获取口味信息
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper();
        lqw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        DishDto dishDto=new DishDto();
//        拷贝到dishDto
        BeanUtils.copyProperties(dish,dishDto);
//        BeanUtils.copyProperties(flavors,dishDto);必须从对象到对象
        dishDto.setFlavors(flavors);
        return dishDto;
    }
    //    修改菜品和对应的口味信息
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        this.updateById(dish);
//        口味表要先把原来的删除之后再新增
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lqw);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            dishFlavorService.save(flavor);
        }
    }

    //    删除菜品和口味信息

    @Transactional
    @Override
    public void deleteWithFlavor(List<Long> ids) {
        this.removeByIds(ids);
        for (Long id : ids) {
            LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper();
            lqw.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(lqw);
        }
        return;
    }
}
